package com.meihua.baoleyi.ui;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.CommonAdapter;
import com.meihua.baoleyi.base.BaseRecyclerViewActivity;
import com.meihua.baoleyi.bean.BillBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.view.IRecyclerView;
import com.meihua.baoleyi.view.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 账单
 * Created by liming on 2017/10/18
 */
public class MyWalletBillActivity extends BaseRecyclerViewActivity {

    @Bind(R.id.text_back)
    ImageView textBack;
    @Bind(R.id.text_title)
    TextView textTitle;
    @Bind(R.id.bill_recyclerview)
    IRecyclerView mRecyclerview;
    private CommonAdapter adapter;
    private List<BillBean.DatasBean> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);
        ButterKnife.bind(this);
        textTitle.setText("账单");
        initRecyclerView(mRecyclerview);
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        mData = new ArrayList<>();
        adapter = new CommonAdapter(MyWalletBillActivity.this, mData);
        return adapter;
    }

    @Override
    protected void getServiceData(final int page) {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<String, String>() {
            {
                put("user_id", myApp.getUser_id());
                put("page", page + "");
            }
        };
        NetHelper.get(APIServer.MYCHECK, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                BillBean bean = NetHelper.getBeanForJson(BillBean.class, s);
                if (bean == null) return;
                if(bean.getData().size()!=0){
                    if(page == 1){
                        /*刷新*/
                        mRecyclerview.setRefreshing(false);
                        mData.clear();
                    }
                    mData.addAll(bean.getData());
                    adapter.notifyDataSetChanged();
                    mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
                }else {
                    mRecyclerview.setRefreshing(false);
                    mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                }
                dismissDialog();
            }
        });
    }

    @OnClick({R.id.text_back, R.id.text_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_back:
                finish();
                break;
        }
    }
}
